package com.atguigu.petstore.shop.service.impl;
import java.math.BigDecimal;

import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.coupon.vo.SeckillProductVo;
import com.atguigu.petstore.feign.coupon.CouponFeignClient;
import com.atguigu.petstore.shop.entity.ShopProductEntry;
import com.atguigu.petstore.shop.entity.ShopProductImg;
import com.atguigu.petstore.shop.service.ShopProductEntryService;
import com.atguigu.petstore.shop.service.ShopProductImgService;
import com.atguigu.petstore.shop.vo.ProductPublishVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.petstore.shop.entity.ShopProduct;
import com.atguigu.petstore.shop.service.ShopProductService;
import com.atguigu.petstore.shop.mapper.ShopProductMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author lfy
* @description 针对表【shop_product(商城-商品表)】的数据库操作Service实现
* @createDate 2023-09-24 16:46:17
*/
@Service
public class ShopProductServiceImpl extends ServiceImpl<ShopProductMapper, ShopProduct>
    implements ShopProductService{


    @Resource
    ShopProductMapper shopProductMapper;

    @Autowired
    ShopProductEntryService productEntryService;

    @Autowired
    ShopProductImgService productImgService;

    @Autowired
    CouponFeignClient couponFeignClient;




    @Transactional
    @Override
    public void publishProduct(ProductPublishVo vo) {
        ProductPublishVo.ProductImagesDTO images = vo.getProductImages();
        List<ProductPublishVo.ProductEntryDTO> entry = vo.getProductEntry();
        ProductPublishVo.ProductBaseInfoDTO baseInfo = vo.getProductBaseInfo();


        //1、保存商品基本信息
        ShopProduct product = new ShopProduct();
        product.setProductName(baseInfo.getProductName());
        product.setCateId(baseInfo.getCateId());
        product.setSubTitle(baseInfo.getSubTitle());
        product.setProductImg(baseInfo.getProductImg());
        product.setPrice(baseInfo.getPrice());
        product.setOriginPrice(baseInfo.getPrice());
        product.setWeight(baseInfo.getWeight());
        product.setSaleCount(0);
        product.setStockCount(baseInfo.getStockCount());

        shopProductMapper.insert(product);

        //拿到商品的自增id
        Long productId = product.getId();


        //2、保存商品套餐
        List<ShopProductEntry> entries = new ArrayList<>();
        //拿到前端提交的所有套餐
        for (ProductPublishVo.ProductEntryDTO dto : entry) {
            ShopProductEntry shopProductEntry = new ShopProductEntry();
            //封装前端的数据到数据库的Entity模型中
            shopProductEntry.setProductEntryName(dto.getEntryName());
            //回填商品id
            shopProductEntry.setProductId(productId);
            shopProductEntry.setSaleCount(0);
            shopProductEntry.setStockCount(dto.getStockCount());
            shopProductEntry.setEntryPrice(dto.getPrice());
            shopProductEntry.setEntryOriginPrice(dto.getPrice());

            entries.add(shopProductEntry);
        }

        productEntryService.saveBatch(entries);


        //3、商品的图片
        List<ShopProductImg> imgs = new ArrayList<>();
        // 遍历封装前端提交的缩略图
        for (String img : images.getThumbnail()) {
            ShopProductImg productImg = new ShopProductImg();
            productImg.setImgUrl(img);
            //回填商品id
            productImg.setProductId(productId);
            productImg.setImgType("1");


            imgs.add(productImg);
        }

        // 遍历封装前端提交的详情图
        for (String img : images.getDetailImgs()) {
            ShopProductImg productImg = new ShopProductImg();

            productImg.setImgUrl(img);
            //回填商品id
            productImg.setProductId(productId);
            productImg.setImgType("2");

            imgs.add(productImg);
        }

        productImgService.saveBatch(imgs);


    }

    @Override
    public List<ShopProduct> getSeckillProduct() {
        List<ShopProduct> list = new ArrayList<>();

        //1、找优惠服务要到所有参与秒杀的商品id，以及价格
        Result<List<SeckillProductVo>> product = couponFeignClient.getSeckillProduct();

        for (SeckillProductVo vo : product.getData()) {
            ShopProduct shopProduct = getById(vo.getProductId());

            //设置为秒杀优惠价
            shopProduct.setPrice(vo.getUsedPrice());

            list.add(shopProduct);
        }

        //2、去自己的数据库查到这些商品的详情信息，封装返回给app
        return list;
    }

    @Override
    public List<ShopProduct> getRankProduct() {


        return  shopProductMapper.getRankProducts();
    }
}




